package jgl.vajra.management.part.mine.activity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Map;

import io.reactivex.Observable;
import jgl.vajra.management.R;
import jgl.vajra.management.bean.CustomerRegistrationBean;
import jgl.vajra.management.databinding.ActivityCustomerRegistrationBinding;
import jgl.vajra.management.event.CustomerRegistrationEvent;
import jgl.vajra.management.event.MyCarListEvent;
import jgl.vajra.management.manager.AccountManager;
import jgl.vajra.management.manager.IntentManager;
import jgl.vajra.management.part.home.pop.AuthenticationScreeningPop;
import jgl.vajra.management.part.home.pop.CustomerRegistrationPop;
import jgl.vajra.management.part.mine.contract.CustomerRegistrationContract;
import jgl.vajra.management.part.mine.viewModel.CustomerRegistrationViewModel;
import jgl.vajra.management.widget.bean.BaseListData;
import jgl.vajra.management.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.management.widget.interfaces.RefreshRecyclerLoadStatusListener;
import jgl.vajra.management.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.management.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.management.widget.recyclerView.RefreshRecyclerNetConfig;
import jgl.vajra.management.widget.utils.UIUtils;

@CreateViewModel(CustomerRegistrationViewModel.class)
public class CustomerRegistrationActivity extends BaseMVVMActivity<CustomerRegistrationViewModel, ActivityCustomerRegistrationBinding>implements CustomerRegistrationContract.View {

    private int internal;
    private String city="";
    private String province="";
    private String type="";

    @Override
    public int getLayoutId() {
        return R.layout.activity_customer_registration;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);
        internal = getIntent().getIntExtra("internal", 0);

        mBinding.toolbar.setNavigationIcon(R.drawable.ic_left_arrow);
        mBinding.toolbar.setNavigationOnClickListener(view -> finish());


        mBinding.recyclerView.setRefreshRecyclerNetConfig(new RefreshRecyclerNetConfig() {
            @Override
            public Observable getNetObservable(Map<String, Object> map, int action) {
                map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
                map.put("internal", internal);
                map.put("pageSize", 30);
                map.put("city", city);
                map.put("province", province);
                map.put("type", type);
                return mViewModel.getCustomerRegistrationList(map);
            }
        });


        SingleTypeBindingAdapter adapter = new SingleTypeBindingAdapter(this, null, R.layout.item_customer_registration);
        mBinding.recyclerView.setAdapter(adapter);
        mBinding.recyclerView.setIsDataObject(true);
        mBinding.recyclerView.setRefreshLoaderMore(false, false);
        mBinding.recyclerView.setRefreshRecyclerLoadStatusListener(new RefreshRecyclerLoadStatusListener<BaseListData<CustomerRegistrationBean>>() {
            @Override
            public void onSucceed(BaseListData<CustomerRegistrationBean> data, int action) {
                /*for (int i = 0; i < data.data.size(); i++) {
                    data.data.get(i).type = type;
                }*/

                mBinding.recyclerView.setRefreshLoaderMore(true, true);

                mBinding.recyclerView.requestNetObjectSuccess(data.data, action);
            }

            @Override
            public void onError(String errorInfo, int errorCode, int action) {
                super.onError(errorInfo, errorCode, action);
//                mBinding.recyclerView.requestNetError(errorInfo, errorCode);
            }
        });
        mBinding.recyclerView.firstLoad();


    }

    @Override
    public void onSearch() {
        IntentManager.toCustomerRegistrationSearchActivity(this, internal);
    }

    @Override
    public void onAddMyCar() {
        CustomerRegistrationPop customerRegistrationPop = new CustomerRegistrationPop(this);
        customerRegistrationPop.showPopupWindow(mBinding.toolbar);
    }


    @Override
    protected boolean isRegisterEvent() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomerRegistrationEvent(CustomerRegistrationEvent event) {
        city = event.city;
        province = event.province;
        type = event.type;
        mBinding.recyclerView.firstLoad();
    }
}